package com.example.zuo.mysuperwidgetdemo.ui;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.view.View;

import com.example.zuo.mysuperwidgetdemo.R;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.ActionCenter;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.Builder;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.ConvertAction;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.SuperAdapterWrapper;
import com.example.zuo.mysuperwidgetdemo.ui.base.BaseNewActivity;
import com.example.zuo.mysuperwidgetdemo.ui.imageviewplay.ImageviewActivity;
import com.example.zuo.mysuperwidgetdemo.ui.imageviewplay.ZoomImageviewActivity;
import com.example.zuo.mysuperwidgetdemo.ui.refresh.RecyclerviewActivity;
import com.example.zuo.mysuperwidgetdemo.ui.test.TestActivity;
import com.example.zuo.mysuperwidgetdemo.utils.ToastUtils;
import com.example.zuo.mysuperwidgetdemo.widget.DividerItemDecoration;
import com.socks.library.KLog;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends BaseNewActivity {
    //view
    RecyclerView recyclerView;
    ////
    String[] strings = {"上拉加载下拉刷新", "edittext测试", "图片轮播", "缩放imageview"};
    List<String> list = new ArrayList<>();

    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void bindId() {
        recyclerView = getView(R.id.recyclerView);
    }

    @Override
    protected void init() {
        getBase();
        for (String s : strings) {
            list.add(s);
        }
        Intent i_getvalue = getIntent();
        String action = i_getvalue.getAction();

        if (Intent.ACTION_VIEW.equals(action)) {

            Uri uri = i_getvalue.getData();
            if (uri != null) {
                String name = uri.getQueryParameter("name");
                String age = uri.getQueryParameter("age");
                ToastUtils.showCustomToast(this, "name=" + name + ";age=" + age);
            }
        }
        showData();
    }

    private void getBase() {
        String str = "Hello!";
        // String strBase64 = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
        String strBase64 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC322qiSggKZ6HysG2nvn90l1V+lH2vSwZI7e5YgkGg+9OkYUR5mkETtL2kHa0Eqr73lULRUEhyA+xEme69gbv+ymxQdOozNdQMJr5f6dg06t8TF22f6mtVMHXeU+OJ0M4EATtq+u57cfZuO+rISJ/5qDfZXBwp6iHfZkWy46hsFQIDAQAB";
        String str2 = null;
        try {
            str2 = new String(Base64.decode(strBase64.getBytes("ISO-8859-1"), Base64.DEFAULT),"ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] sss = new byte[0];
        try {
            sss = str2.getBytes("ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        KLog.e(Arrays.toString(sss));
    }


    @Override
    protected void setListener() {

    }

    private void showData() {
        SuperAdapterWrapper wrapper;
        Builder builder = new Builder(this);

        wrapper = builder
                .addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL, R.color.white, 5))
                .defalutItemLayout(R.layout.item_main_layout)
                .showIn(recyclerView)
                .addAction(ActionCenter.CONVERT_COMMAND, new ConvertAction() {
                    @Override
                    public void convert(ViewHolder holder, Object t, final int position) {

                        holder.setText(R.id.name, (String) t);
                        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                switch (position) {
                                    case 0:
                                        Intent intent = new Intent(MainActivity.this, RecyclerviewActivity.class);
                                        startActivity(intent);
                                        break;
                                    case 1:
                                        Intent intent1 = new Intent(MainActivity.this, TestActivity.class);
                                        startActivity(intent1);
                                        break;
                                    case 2:
                                        Intent intent2 = new Intent(MainActivity.this, ImageviewActivity.class);
                                        startActivity(intent2);
                                        break;
                                    case 3:
                                        Intent intent3 = new Intent(MainActivity.this, ZoomImageviewActivity.class);
                                        startActivity(intent3);
                                        break;
                                    default:
                                        break;
                                }


                            }
                        });
                    }
                }).build();
        wrapper.handleData(list, true);
    }

}
